//
//  PreferenceGoodsVC.m
//  NormalFramework
//
//  Created by beisheng03 on 2018/3/16.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "PreferenceGoodsVC.h"
#import "XMGRefreshHeader.h"
#import "XMGRefreshFooter.h"
#import "HomeCollectionCell.h"
#import "GoodsInfoModel.h"

#import "GoodDetailController.h"
#import "BaseNavigationController.h"
@interface PreferenceGoodsVC ()
@property (nonatomic, strong) NSMutableArray<GoodsInfoModel *> *dataModels;

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;
@end

@implementation PreferenceGoodsVC

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"优先商品";
    

    [self.collectionView  registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:[HomeCollectionCell identifierStr]];

    MJWeakSelf;
    
    weakSelf.collectionView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
    }];
    
    [weakSelf.collectionView.mj_header beginRefreshing];
    
    
    weakSelf.collectionView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
        
    }];
}






- (void)loadData{
    self.curpage = 1;
    
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    
    parmars[@"curpage"] = @"1";
    
    parmars[@"is_recommend"] = @"1";
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/get_more" parameters:parmars completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            NSLog(@"%@",[responseObject mj_JSONString]);
            
            weakSelf.pageCount = responseObject[@"data"][@"count"];
            
            weakSelf.dataModels = [GoodsInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
            [weakSelf.collectionView.mj_footer resetNoMoreData];
            
            if ([weakSelf.pageCount isEqualToString:@"1"]) {
                
                [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
            [weakSelf.collectionView.mj_header endRefreshing];
            
            [weakSelf.collectionView reloadData];
            
        } else {
             [weakSelf.collectionView.mj_header endRefreshing];
            
            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
        
        
        
    }];
    
    
}





- (void)loadMoreData
{
    self.curpage ++;
    
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    
    parmars[@"curpage"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    parmars[@"is_recommend"] = @"1";

    
    
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/get_more" parameters:parmars completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            
            NSMutableArray<GoodsInfoModel *> *temp = [GoodsInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.collectionView.mj_footer endRefreshing];
                
                
            }
            
            
            
            [weakSelf.collectionView reloadData];
            
        } else {
            
            [weakSelf.collectionView.mj_footer endRefreshing];
            
            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
        
        
        
    }];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    if (self.dataModels == nil) {
        return 0;
    } else {
        return 1;
    }
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataModels == nil) {
        return 0;
    } else {
        return self.dataModels.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeCollectionCell identifierStr] forIndexPath:indexPath];
    
    cell.model = self.dataModels[indexPath.item];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodDetailController *temp = [[GoodDetailController alloc]init];
  
    
    [self.navigationController pushViewController:temp animated:YES];
    
    
}

@end
